import os
import re

def update_par_file(main_file,out_file,old_line,new_line,):
    # modify the new par file
    StartPar = open(main_file,"r")
    StartPar_text = StartPar.read()
    # create new par file name
    # 重新命名输出
    NewPar_filename = out_file
    NewPar=open(NewPar_filename,"w")
    # replace lines in base parfile
    # 修改某一行
    Old_lines=old_line # Finds this line in the "main_file" to be modified
    New_lines=new_line # Replaces this line in the updated par file
    NewPar_text=StartPar_text.replace(Old_lines,New_lines)
    # write and close new par file
    NewPar.write(NewPar_text)

# number:设置%5 或是 %10 的参数
# new_par_name :输入数字 5 或是10 控制生成文件名
def generate_5_10(par_name,new_par_name,number,project_file_path):
    out_file_par=None
    # file_path='simulation.par'

    # 直接判断文件是否为空就行了
    string3=''

    if len(os.listdir('./first_par_result'))==0 and len(os.listdir('./two_par_result'))==0:
        string3='./'
    if len(os.listdir('./two_par_result'))==0 and len(os.listdir('./first_par_result'))!=0:
        string3='./first_par_result/'
    if len(os.listdir('./first_par_result'))==0 and len(os.listdir('./two_par_result'))!=0:
        string3='./two_par_result/'
    if len(os.listdir('./first_par_result'))!=0 and len(os.listdir('./two_par_result'))!=0:
        string3='./two_par_result/'

    file_path=par_name
    # print(f'{string3}'+file_path)
    # if os.path.isfile(f'./two_par_result/'+file_path):
    if os.path.isfile(f'{string3}'+file_path):
        # print("ok")
        # file_par =  './two_par_result/'+file_path # 替换为你的文件路径
        file_par =  f'{string3}'+file_path # 替换为你的文件路径
        with open(file_par, "r") as file:
            file_lines = file.readlines()
            # pattern = "Parameter EmissionAngle\s*"
            pattern = "Parameter EmissionAngle "
            old_matches = [line for line in file_lines if re.search(pattern, line, re.MULTILINE)]
            # print(old_matches)  #['Parameter EmissionAngle 0.0 60.0 15.0 deg\n']
            if old_matches:
                for old_line in old_matches:
                    list_EmissionAngle=old_line.split()
                    # print(list_EmissionAngle)
                    new_line=''
                    for ls_EmissionAngle in list_EmissionAngle:
                        if ls_EmissionAngle =='EmissionAngle':
                            new_line+='Disabled'+' '+ls_EmissionAngle+' '
                        else:
                            new_line+=ls_EmissionAngle+' '
                    new_line+='\n'
                # print(new_line)#Parameter Disabled EmissionAngle 0.0 60.0 15.0 deg
                        # new_line+="Parameter Disabled EmissionAngle 0.0 60.0 15.0 deg\n"
                out_file_par=f'{project_file_path}/enter/new_{par_name.split("_Ref.")[0]}_{new_par_name}.par'
                # print(out_file_par)
                update_par_file(file_par,out_file_par,old_line,new_line)


    # print(out_file_par)
    file_path1=out_file_par
    if os.path.isfile(file_path1):
        # print("ok")
        file_par1 =  file_path1
        with open(file_par1, "r") as new_file:
            file_lines1 = new_file.readlines()
            #SiON-B.d
            string1='SiON-B'
            string2='.d'
            pattern1 = "Parameter "+string1+string2
            # print(pattern1)
            old_matches1 = [line for line in file_lines1 if re.search(pattern1, line, re.MULTILINE)]
            list1=[]
            # print(old_matches1)
            if old_matches1:
                for old_line1 in old_matches1:
                    # print(old_line1)#Parameter SiON-B.d 1000.0 1100.0 50.0 nm
                    words=old_line1.split()
                    # print(words)
                    #如果是数字则转成float类型
                    new_lst = [float(i) if i.replace('.', '', 1).isdigit() else i for i in words]
                    for line in new_lst:
                        if type(line) is float:
                            num=1
                            new_string=f'line_{num}'
                            new_string=line*number
                            # print(new_string)
                            num+=1
                            list1.append(new_string)
                    # print(list1)#[950.0, 1045.0, 47.5]
                #拼接计算过后的字符串
                new_line2 = "Parameter " + string1 + string2
                for ls in list1:
                    new_line2 += ' '+str(ls)
                new_line2=new_line2+' '+'nm\n'
                # print(new_line2)
                out_file_par='./finally_par_result/'+par_name.split("_Ref.")[0]+f"_{new_par_name}"+'.par'
                update_par_file(file_par1,out_file_par,old_line1,new_line2)
            else:
                # print("进入else了")
                # 如果Parameter SiON-B.d 1000.0 1100.0 50.0 nm 不存在
                # 怎则计算上面 Layer SiON-B 1050 nm n File "C:/Users/admin/Desktop/01 Adjust&Data/00 NK/TFE4CVD1-2.txt"
                pattern1 = "Layer "+string1
                # print(pattern1)
                old_matches2 = [line for line in file_lines1 if re.search(pattern1, line, re.MULTILINE)]
                if old_matches2:
                    for old_line2 in old_matches2:
                        # print(old_line2)
                        words1=old_line2.split()
                        # print(words1)
                        new_lst = [float(i) if i.replace('.', '', 1).isdigit() else i for i in words1]
                        for line in new_lst:
                            if type(line) is float:
                                new_string1=line*number
                                # print(new_string)
                    #Layer SiON-B 1050 nm n File "C:/Users/admin/Desktop/01 Adjust&Data/00 NK/TFE4CVD1-2.txt"
                    new_line3=''
                    for line4 in words1:
                        if line4==string1:
                            new_line3+=' '+line4+' '+str(new_string1)+' '
                        elif line4.isdigit():
                            continue
                        else:
                            new_line3+=line4+' '
                    new_line3+="\n"
                    # new_line3 = "Layer "+string1+' '+str(new_string1)+' '+'nm n File "C:/Users/admin/Desktop/01 Adjust&Data/00 NK/TFE4CVD1-2.txt"\n'
                    # print(new_line3)
                    out_file_par='./finally_par_result/'+par_name.split("_Ref.")[0]+f"_{new_par_name}"+'.par'
                    update_par_file(file_par1,out_file_par,old_line2,new_line3)













# generate_5_10("0New_B1.par",5,0.95)
# list1=['0New_B1.par', '1New_B1.par', '2New_B1.par', '3New_B1.par', '0New_B2.par', '1New_B2.par', '2New_B2.par', '3New_B2.par']
# for lst in list1:
#     generate_5_10(lst,10,0.9)
                
                        
                        
                            



